﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Core;
using dtSearch32Lib;
using DocumentSearchServiceModel;
using DocumentSearch32Lib;
using Portal.Systems.Model;
using Portal.Workflow.Bussiness;
using Portal.Cms.Utility;
using System.Data;
using Portal.Database.Service;
using System.Collections.ObjectModel;

namespace Portal.Cms.Render
{
    public class AddCmsDataRenderBll
    {
        static string GetPlugInJavascript(string SiteId,string ParentDataId)
        {
            string ScriptText =
                "<input type=hidden id=txtParentDataId value=\"" + ParentDataId + "\">\r\n" +
                   "<script language=javascript>\r\n" +
                   "    function ProcessHotCategory(Id)\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        checked=document.getElementById('chkHot'+Id).checked;\r\n"+
                   "        CheckedIds=document.getElementById('txtCheckedHotCategory').value;\r\n" +
                   "        CheckedIds = Portal.Cms.Render.AddCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,checked).value;\r\n" +
                   "        document.getElementById('txtCheckedHotCategory').value=CheckedIds;\r\n" +

                   
                   "        if(checked)\r\n"+
                   "        {\r\n"+
                   "            document.getElementById('chk'+Id).checked=true;\r\n"+

                   "            CheckedIds=document.getElementById('txtCheckedCategory').value;\r\n" +
                   "            CheckedIds = Portal.Cms.Render.AddCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,true).value;\r\n" +
                   "            document.getElementById('txtCheckedCategory').value=CheckedIds;\r\n" +


                   "        }\r\n"+
                   "    }\r\n"+

                   "    function ProcessCheckCategory(Id)\r\n" +
                   "    {\r\n" +
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        checked=document.getElementById('chk'+Id).checked;\r\n" +
                   "        CheckedIds=document.getElementById('txtCheckedCategory').value;\r\n" +
                   "        CheckedIds = Portal.Cms.Render.AddCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,checked).value;\r\n" +
                   "        document.getElementById('txtCheckedCategory').value=CheckedIds;\r\n"+
                   "        if(checked==false)\r\n" +
                   "        {\r\n" +
                   "            document.getElementById('chkHot'+Id).checked=false;\r\n" +


                   "            CheckedIds=document.getElementById('txtCheckedHotCategory').value;\r\n" +
                   "            CheckedIds = Portal.Cms.Render.AddCmsDataRenderBll.ServerSideProcessCheckCategory(SiteId,CheckedIds,Id,false).value;\r\n" +
                   "            document.getElementById('txtCheckedHotCategory').value=CheckedIds;\r\n" +


                   "        }\r\n" +
                   "    }\r\n" +
                   "</script>\r\n";
            return ScriptText;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideProcessCheckCategory(string SiteId,string CheckedIds,string Id,bool Checked)
        {
            if (Checked)
            {
                if (!string.IsNullOrEmpty(CheckedIds))
                {
                    CheckedIds = CheckedIds + ";";
                }
                CheckedIds = CheckedIds + Id;
            }
            else
            {
                CheckedIds = CheckedIds.Replace(Id,"");
            }
            CheckedIds = CheckedIds.Replace(";;", ";");
            if (CheckedIds.Length > 0)
            {
                if (CheckedIds[0].ToString().Equals(";"))
                {
                    CheckedIds = CheckedIds.Substring(1);
                }
            }
            return CheckedIds;
        }


        

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string ParentDataId = (string)WebEnvironments.Request(SiteId, "parent");
                string wfcId = WebConfig.WorkFlowId;
                if (!string.IsNullOrEmpty(WebEnvironments.Request(SiteId, "wfc")))
                    wfcId = WebEnvironments.Request(SiteId, "wfc");

                if (string.IsNullOrEmpty(wfcId)) wfcId = "";
                string Html =
                   GetPlugInJavascript(SiteId, ParentDataId) +
                   DrawAddForm(SiteId, GroupDataId, wfcId).HtmlContent;
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            
            return OAjaxOut;
        }
        public static string BaseSiteUrl
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
                return baseUrl;
            }
        }

        public static string ConvertUrl(string Url)
        {
            Url = Url.Replace("192.168.1.2/", BaseSiteUrl + "/");
            Url = Url.Replace("117.3.4.207/", BaseSiteUrl + "/");
            return Url;
        }


        public static string GetSelect(string SiteId, string FileName, string ComboboxId,string FocusId)
        {
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string Xml = OSiteParam.PathRoot + "\\Admin\\XmlData\\News\\" + FileName;
                DataSet ds = new DataSet();
                ds.ReadXml(Xml);
                string SelectData =
                    " <select id=\"" + ComboboxId + "\" class=drplist>\r\n" +
                    "   <option value=\"\"></option>\r\n";
                for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
                {
                    string Id = (string)ds.Tables[0].Rows[iIndex]["Id"];
                    string Name = (string)ds.Tables[0].Rows[iIndex]["Name"];
                    if (FocusId.Equals(Id))
                    {
                        SelectData += "<option selected value=\"" + Id + "\">" + Name + "</option>\r\n";
                    }
                    else
                    {
                        SelectData += "<option value=\"" + Id + "\">" + Name + "</option>\r\n";
                    }
                }
                SelectData += " </select>\r\n";
                return SelectData;
            }
            catch
            {
                return "";
            }
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildCategory(
            string SiteId,
            string DataLanguageId,
            string Categories, 
            string TreeViewId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                if (string.IsNullOrEmpty(Categories))
                {
                    OAjaxOut.HtmlContent = "Chưa thiết lập nhóm cho vị trí này";
                    return OAjaxOut;
                }
                GroupDataCategoryCls[] GroupDataCategories = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataCategory(SiteId, DataLanguageId);
                
                //string XmlFile = OSiteParam.PathRoot + "\\admin\\xmldata\\news\\Category\\"+DataLanguage+"\\Category.xml";
                //DataSet ds = new DataSet();
                //ds.ReadXml(XmlFile);
                //DataView dv = new DataView(ds.Tables[0], "ParentId='0'", "", DataViewRowState.CurrentRows);
                string Html =
                    "<div class=CssListField>\r\n" +
                    "<ul id=\""+TreeViewId+"\" class=\"TreeView\">\r\n";
                for (int iIndex = 0; iIndex < GroupDataCategories.Length; iIndex++)
                {
                    string Id = GroupDataCategories[iIndex].GroupDataCategoryId;// dv[iIndex]["Id"].ToString();
                    string Name = GroupDataCategories[iIndex].Name;// dv[iIndex]["Name"].ToString();
                    //DataView dvSub = new DataView(ds.Tables[0], "ParentId='" + Id + "'", "", DataViewRowState.CurrentRows);
                    if (GroupDataCategories[iIndex].SubGroupDataCategories.Count>0)
                    {
                        bool Found = false;
                        AjaxOut OAjaxOutReadingSubField = ServerSideReadingSubField(SiteId,GroupDataCategories[iIndex].SubGroupDataCategories, DataLanguageId, Categories, Id);
                        Found = OAjaxOutReadingSubField.RetBoolean;
                        if (Found)
                        {
                            Html += " <li id=\"" + Id + "\" class=\"Expanded\">\r\n" +
                                        "<a href=\"javascript:ProcessExpandCollapse('" + Id + "');\"><span id=\"spanMainFieldName" + Id + "\">" + Name + "</span></a>\r\n" +
                                            "<input type=hidden value=\"0\" id=\"txtReadSubField" + Id + "\"><ul id=\"ulSubField" + Id + "\">\r\n" +
                                            OAjaxOutReadingSubField.HtmlContent +
                                            "</ul>\r\n" +
                                        "</li>\r\n";
                        }
                    }
                    else
                    {
                        if (Categories.IndexOf("$" + Id + "$") != -1)
                        {
                            Html += " <li id=\"" + Id + "\" class=\"Expanded\">\r\n" +
                                    "<input type=checkbox class=checkbox onchange=\"javascript:ProcessCheckCategory('" + Id + "','')\" id=\"chk" + Id + "\" style=\"margin-right:4px;\"><span id=\"spanMainFieldName" + Id + "\">" + Name + "</span><input id=\"chkHot" + Id + "\" style=\"margin-left:5px;\" type=checkbox class=checkbox onchange=\"javascript:ProcessHotCategory('" + Id + "');\">Nóng chuyên mục\r\n" +
                                            "<input type=hidden value=\"0\" id=\"txtReadSubField" + Id + "\"><ul id=\"ulSubField" + Id + "\">\r\n" +

                                            "</ul>\r\n" +
                                        "</li>\r\n";
                        }
                    }
                }
                Html += "</ul>\r\n" +
                      "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingSubField(
            string SiteId,
            Collection<GroupDataCategoryCls> SubGroupDataCategories,
            string DataLanguageId,
            string Categories, 
            string CategoryId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                //string XmlFile = OSiteParam.PathRoot + "\\admin\\xmldata\\news\\Category\\" + DataLanguage + "\\Category.xml";
                //DataSet ds = new DataSet();
                //ds.ReadXml(XmlFile);
                //DataView dv = new DataView(ds.Tables[0], "ParentId='"+CategoryId+"'", "", DataViewRowState.CurrentRows);
     
                string Html = "";
                bool Found = false;
                for (int jIndex = 0; jIndex < SubGroupDataCategories.Count; jIndex++)
                {
                    string Id = SubGroupDataCategories[jIndex].GroupDataCategoryId;
                    string Name = SubGroupDataCategories[jIndex].Name;
                    if (Categories.IndexOf("$" + Id + "$") != -1)
                    {
                        Html += " <li class=liSubTreeViewItem>\r\n" +
                                "       <input type=checkbox class=checkbox onchange=\"javascript:ProcessCheckCategory('" + Id + "','')\" id=\"chk" + Id + "\" style=\"margin-right:4px;\"><span id=\"spanMainFieldName" + Id + "\">" + Name + "</span><input id=\"chkHot" + Id + "\" style=\"margin-left:5px;\" type=checkbox class=checkbox onchange=\"javascript:ProcessHotCategory('" + Id + "');\">Nóng chuyên mục\r\n" +
                                " </li>\r\n";
                        Found = true;
                    }
                }
                OAjaxOut.RetNumber = SubGroupDataCategories.Count;
                OAjaxOut.HtmlContent = Html;
                OAjaxOut.RetBoolean = Found;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }




        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(
            string SiteId,
            string GroupDataId,
            string wfcId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                WorkflowControlCls OWorkflowControl = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateWorkflowControlModel(SiteId, wfcId);
                WorkflowDefCls OWorkflowDef = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateWorkflowDefFromWorkflowControl(SiteId, wfcId);

                string DataLanguage = "";
                string WorkflowName = "";
                string WorkflowDefId = "";
                if (OWorkflowDef != null)
                {
                    WorkflowName = " - theo quy trình " + OWorkflowControl.Title;
                    WorkflowDefId = OWorkflowDef.WorkflowDefId;
                    DataLanguage = OWorkflowControl.DataLanguageCode;
                    //kiem tra xem role nay co quyen start quy trinh nay khong
                    bool Allow = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckAllowAccessWorkflow(SiteId, WebSessionUtility.GetCurrentLoginUser().RoleId, WorkflowDefId);
                    if (Allow == false) throw new Exception("Bạn không có quyền thao tác với quy trình này!");
                }
                else
                {
                    SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                }
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);


                string BackScript = "javascript:window.open('InputData.aspx?gid=" + OGroupData.GroupDataId + "','_self');";

                //BackScript = "javascript:window.open('../../home.aspx','_self');";

                string NewDataId = System.Guid.NewGuid().ToString();
                string Html =
                     "<script language=javascript>\r\n " +

                    "  function ProcessExpandCollapse(Id)\r\n" +
                    "  {\r\n" +
                    "     if(document.getElementById(Id).className=='Collapsed')\r\n" +
                    "     {\r\n" +
                    "         document.getElementById(Id).className='Expanded';\r\n" +
                    "     }\r\n" +
                    "     else\r\n" +
                    "     {\r\n" +
                    "         document.getElementById(Id).className='Collapsed';\r\n" +
                    "     }\r\n" +
                    "  }\r\n" +

                     " function CallSaveAddData()\r\n" +
                     " {\r\n" +
                     "      document.getElementById('divCallSaveAddDataProcessing').innerHTML='Tiến hành cập nhật và đánh index tìm kiếm...';\r\n" +
                     "      setTimeout('RealCallSaveAddData()',10);\r\n" +
                     " }\r\n" +

                     "  function RealCallSaveAddData()\r\n" +
                     "  {\r\n" +
                     "      SiteId='" + SiteId + "';\r\n" +
                     "      GroupDataId='" + GroupDataId + "';\r\n" +
                     "      DataId=document.getElementById('txtDataId').value;\r\n" +
                     "      Alias = document.getElementById('txtAlias').value;\r\n" +
                     //"      Subject = document.getElementById('txtSubject').value;\r\n" +
                     "      Subject = Alias;\r\n" +

                     "      OtherSubject= '';\r\n" +
                     "      Description = '';\r\n" +
                     //"      if(FCKeditorAPI!=null){\r\n" +
                     //"          oEditor = FCKeditorAPI.GetInstance('FCKeditorSapo');\r\n" +
                     //"          if(oEditor!=null)\r\n" +
                     //"          {\r\n" +
                     //"              Description= oEditor.GetXHTML( true ); \r\n" +
                     //"          }\r\n" +
                     //"      }\r\n" +
                     "      if(document.getElementById('txtDescription')!=null)\r\n" +
                     "      {\r\n" +
                     "          Description = document.getElementById('txtDescription').value; \r\n" +
                     "      }\r\n" +

                     "      Body = '';\r\n" +
                     //"      if(FCKeditorAPI!=null){\r\n" +
                     //"          oEditor = FCKeditorAPI.GetInstance('FCKeditorBody');\r\n" +
                     //"          Body= oEditor.GetXHTML( true ); \r\n" +
                     //"      }\r\n" +                    
                     "      Source = '';\r\n" +
                     "      Url = document.getElementById('txtUrl').value;\r\n" +
                      "          Target = '';\r\n" +
                     "      if(document.getElementById('txtTarget')!=null)\r\n" +
                      "      {\r\n" +
                     "          Target = document.getElementById('txtTarget').value;\r\n" +
                     "        }\r\n" +
                     "      UploadFile='';\r\n" +
                     //"      UploadFile=document.getElementById('txtUploadFile').value;\r\n" +
                     "      SortIndex=document.getElementById('txtSortIndex').value;\r\n" +
                     "      ParentDataId=document.getElementById('txtParentDataId').value;\r\n" +

                     "      DocType='';\r\n" +
                     "      if(document.getElementById('drpSelectDocType')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocType=document.getElementById('drpSelectDocType').value;\r\n" +
                     "      }\r\n" +

                     "      DocField='';\r\n" +
                     "      if(document.getElementById('drpSelectDocField')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocField=document.getElementById('drpSelectDocField').value;\r\n" +
                     "      }\r\n" +

                     "      DocReleaseFrom='';\r\n" +
                     "      if(document.getElementById('drpSelectDocReleaseFrom')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocReleaseFrom=document.getElementById('drpSelectDocReleaseFrom').value;\r\n" +
                     "      }\r\n" +
                     "      DocSigner='';\r\n" +
                     "      if(document.getElementById('drpSelectDocSigner')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocSigner=document.getElementById('drpSelectDocSigner').value;\r\n" +
                     "      }\r\n" +

                     "      DocStatus='';\r\n" +
                     "      if(document.getElementById('drpSelectDocStatus')!=null)\r\n" +
                     "      {\r\n" +
                     "          DocStatus=document.getElementById('drpSelectDocStatus').value;\r\n" +
                     "      }\r\n" +

                     "      ReleaseDate='';\r\n" +
                     "      if(document.getElementById('txtReleaseDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReleaseDate=document.getElementById('txtReleaseDate').value;\r\n" +
                     "      }\r\n" +

                     "      EffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EffectedDate=document.getElementById('txtEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      EndEffectedDate='';\r\n" +
                     "      if(document.getElementById('txtEndEffectedDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          EndEffectedDate=document.getElementById('txtEndEffectedDate').value;\r\n" +
                     "      }\r\n" +

                     "      SignDate='';\r\n" +
                    //"      if(document.getElementById('txtSignDate')!=null)\r\n" +
                    //"      {\r\n" +
                    //"          SignDate=document.getElementById('txtSignDate').value;\r\n" +
                    //"      }\r\n" +

                     "      ReplaceDoc='';\r\n" +
                     "      if(document.getElementById('txtReplaceDoc')!=null)\r\n" +
                     "      {\r\n" +
                     "          ReplaceDoc=document.getElementById('txtReplaceDoc').value;\r\n" +
                     "      }\r\n" +

                     "      ModifyDoc='';\r\n" +
                     "      if(document.getElementById('txtModifyDoc')!=null)\r\n" +
                     "      {\r\n" +
                     "          ModifyDoc=document.getElementById('txtModifyDoc').value;\r\n" +
                     "      }\r\n" +

                     "      VoteType='';\r\n" +
                     "      if(document.getElementById('drpSelectVoteType')!=null)\r\n" +
                     "      {\r\n" +
                     "          VoteType=document.getElementById('drpSelectVoteType').value;\r\n" +
                     "      }\r\n" +

                     "      IsNew = '0';\r\n" +
                     "      Searchable=0;\r\n" +
                     "      if(document.getElementById('drpSelectSearchable')!=null)\r\n" +
                     "      {\r\n" +
                     "          Searchable=document.getElementById('drpSelectSearchable').value;\r\n" +
                     "      }\r\n" +

                     //     Dành cho báo
                     "      CategoryIds='';\r\n" +
                     "      if(document.getElementById('txtCheckedCategory')!=null)\r\n" +
                     "      {\r\n" +
                     "          CategoryIds=document.getElementById('txtCheckedCategory').value;\r\n" +
                     "      }\r\n" +
                     "      HotCategoryIds='';\r\n" +
                     "      if(document.getElementById('txtCheckedHotCategory')!=null)\r\n" +
                     "      {\r\n" +
                     "          HotCategoryIds=document.getElementById('txtCheckedHotCategory').value;\r\n" +
                     "      }\r\n" +
                     "      NewsTypeId='';\r\n" +
                     "      if(document.getElementById('drpSelectNewsType')!=null)\r\n" +
                     "      {\r\n" +
                     "          NewsTypeId=document.getElementById('drpSelectNewsType').value;\r\n" +
                     "      }\r\n" +
                     "      ImageLevelId='';\r\n" +
                     "      if(document.getElementById('drpSelectImageLevel')!=null)\r\n" +
                     "      {\r\n" +
                     "          ImageLevelId=document.getElementById('drpSelectImageLevel').value;\r\n" +
                     "      }\r\n" +
                     "      EventId='';\r\n" +
                     "      if(document.getElementById('drpSelectEvent')!=null)\r\n" +
                     "      {\r\n" +
                     "          EventId=document.getElementById('drpSelectEvent').value;\r\n" +
                     "      }\r\n" +
                     "      ExpImageCount='';\r\n" +
                     "      if(document.getElementById('txtImageCount')!=null)\r\n" +
                     "      {\r\n" +
                     "          ExpImageCount=document.getElementById('txtImageCount').value;\r\n" +
                     "      }\r\n" +
                     "      ExpImageLevelId='';\r\n" +
                     "      if(document.getElementById('drpSelectExploitation')!=null)\r\n" +
                     "      {\r\n" +
                     "          ExpImageLevelId=document.getElementById('drpSelectExploitation').value;\r\n" +
                     "      }\r\n" +
                     "      NewsLevelId='';\r\n" +
                     "      if(document.getElementById('drpSelectNewsLevel')!=null)\r\n" +
                     "      {\r\n" +
                     "          NewsLevelId=document.getElementById('drpSelectNewsLevel').value;\r\n" +
                     "      }\r\n" +
                     "      IsFocus='0';\r\n" +
                     "      if(document.getElementById('chkFocus')!=null)\r\n" +
                     "      {\r\n" +
                     "          if(document.getElementById('chkFocus').checked==true)\r\n" +
                     "          {\r\n" +
                     "              IsFocus='1';\r\n" +
                     "          }\r\n" +
                     "      }\r\n" +
                     "      IsShowAuthor='0';\r\n" +
                     "      if(document.getElementById('chkShowAuthor')!=null)\r\n" +
                     "      {\r\n" +
                     "          if(document.getElementById('chkShowAuthor').checked==true)\r\n" +
                     "          {\r\n" +
                     "              IsShowAuthor='1';\r\n" +
                     "          }\r\n" +
                     "      }\r\n" +
                     "      IsAdv='0';\r\n" +
                     "      if(document.getElementById('chkAdv')!=null)\r\n" +
                     "      {\r\n" +
                     "          if(document.getElementById('chkAdv').checked==true)\r\n" +
                     "          {\r\n" +
                     "              IsAdv='1';\r\n" +
                     "          }\r\n" +
                     "      }\r\n" +
                     "      Keyword='';\r\n" +
                     "      if(document.getElementById('txtKeyword')!=null)\r\n" +
                     "      {\r\n" +
                     "          Keyword=document.getElementById('txtKeyword').value;\r\n" +
                     "      }\r\n" +
                     "      SurveyCode='';\r\n" +
                     "      if(document.getElementById('txtSurveyCode')!=null)\r\n" +
                     "      {\r\n" +
                     "          SurveyCode=document.getElementById('txtSurveyCode').value;\r\n" +
                     "      }\r\n" +
                     "      Note='';\r\n" +
                     "      if(document.getElementById('txtNote')!=null)\r\n" +
                     "      {\r\n" +
                     "          Note=document.getElementById('txtNote').value;\r\n" +
                     "      }\r\n" +
                     "      ExpiredDate='';\r\n" +
                     "      if(document.getElementById('txtExpiredDate')!=null)\r\n" +
                     "      {\r\n" +
                     "          ExpiredDate=document.getElementById('txtExpiredDate').value;\r\n" +
                     "      }\r\n" +

                     "      WorkflowDefId='';\r\n" +
                     "      DataLanguage='';\r\n" +

                     "      Categories='';\r\n" +

                     "      AjaxOut = Portal.Cms.Render.AddCmsDataRenderBll.ServerSideAdd(SiteId, WorkflowDefId, DataLanguage, DataId, GroupDataId, ParentDataId, Alias, Subject, OtherSubject, Description, Body, Source, Url, Target, UploadFile, SortIndex, DocType,DocField, DocReleaseFrom,DocSigner,DocStatus,SignDate, ReleaseDate,EffectedDate,EndEffectedDate,ReplaceDoc,ModifyDoc, VoteType, IsNew, Searchable,  Categories, CategoryIds, HotCategoryIds, NewsTypeId, ImageLevelId, EventId, ExpImageCount, ExpImageLevelId, NewsLevelId, IsFocus, IsShowAuthor, IsAdv, Keyword, SurveyCode, Note, ExpiredDate).value;\r\n" +
                     "      document.getElementById('divCallSaveAddDataProcessing').innerHTML='';\r\n" +
                     "      if(AjaxOut.Error)\r\n" +
                     "      {\r\n" +
                     "          alert(AjaxOut.InfoMessage);\r\n" +
                     "          return;\r\n" +
                     "      }\r\n" +
                     "      if(AjaxOut.RetUrl!='')\r\n" +
                     "      {\r\n" +
                     "          window.open(AjaxOut.RetUrl,'_self');\r\n" +
                     "      }\r\n" +
                     "      else\r\n" +
                     "      {\r\n" +
                     "          document.getElementById('divListForm').innerHTML = AjaxOut.HtmlContent;\r\n" +
                     "          ClientBack();\r\n" +
                     "      }\r\n" +
                     "  }\r\n" +

                   "  </script>\r\n";


                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonRoot = WebConfig.GetButtonIconRoot(OSiteParam);

             
                              
                Html +=
                                                     
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:CallSaveAddData();\"><img class=image src=\"" + ButtonRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"" + BackScript + "\"><img class=image src=\"" + ButtonRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "    <div class=CssFormSaveUpdate>\r\n" +
                   "  <input type=hidden id=txtDataId value=\"" + NewDataId + "\">\r\n" +
                   "  <h3>Tạo mới</h3>\r\n" +
                   "  <div id=divCallSaveAddDataProcessing class=CssProcessing></div>\r\n" +
                   "  <table>\r\n" +
                   "    <tr class=\"CssRowAlias\">\r\n" +
                   "        <td class=c1>Menu hiển thị:</td>\r\n" +
                   "        <td class=c2><input id=txtAlias class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n" +

                    "    <tr class=\"CssRowUrl\">\r\n" +
                   "        <td class=c1>Url:</td>\r\n" +
                   "        <td class=c2><input id=txtUrl class=textbox type='textbox'></td>\r\n" +
                   "    </tr>\r\n" +

                  
                "    <tr class=\"CssRowNote\">\r\n" +
                "        <td class=c1>Ghi chú:</td>\r\n" +
                "        <td class=c2><textarea type=textarea class=textarea id=txtNote></textarea></td>\r\n" +
                "    </tr>\r\n" +
                "    <tr class=\"CssRowSortIndex\">\r\n" +
                "        <td class=c1>Thứ tự:</td>\r\n" +
                "        <td class=c2><input id=txtSortIndex value=\"0\" class=textbox type='textbox'></td>\r\n" +
                "    </tr>\r\n" +
                 "  </table>\r\n" +
                 "</div>\r\n";


                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(
            string SiteId,
            string WorkflowDefId,
            string DataLanguage,
            string DataId,
            string GroupDataId,
            string ParentDataId,
            string Alias,
            string Subject,
            string OtherSubject,
            string Description,
            string Body,
            string Source,
            string Url,
            string Target,
            string UploadFile,
            string SortIndex,
            string DocType,
            string DocField,
            string DocReleaseFrom,
            string DocSigner,
            string DocStatus,
            string SignDate,
            string ReleaseDate,
            string EffectedDate,
            string EndEffectedDate,
            string ReplaceDoc,
            string ModifyDoc,
            string VoteType,
            int IsNew,
            int AttachedSearchable,

            //danh cho bao
            string Categories,
            string CategoryIds,
            string HotCategoryIds,
            string NewsTypeId,
            string ImageLevelId,
            string EventId,
            string ExpImageCount,
            string ExpImageLevelId,
            string NewsLevelId,
            string IsFocus,
            string IsShowAuthor,
            string IsAdv,
            string Keyword,
            string SurveyCode,
            string Note,
            string ExpiredDate)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (string.IsNullOrEmpty(Alias)) Alias = System.Guid.NewGuid().ToString().Substring(0, 8);
                if (string.IsNullOrEmpty(Subject)) throw new Exception("Chưa nhập tiêu đề nhóm dữ liệu!");
                if (!string.IsNullOrEmpty(ReleaseDate))
                {
                    if (FunctionUtilities.checkVnDate(ReleaseDate) == false) throw new Exception("Ngày ban hành không hợp lệ!");
                }
                if (!string.IsNullOrEmpty(EffectedDate))
                {
                    if (FunctionUtilities.checkVnDate(EffectedDate) == false) throw new Exception("Ngày hiệu lực không hợp lệ!");
                }
                if (!string.IsNullOrEmpty(EndEffectedDate))
                {
                    if (FunctionUtilities.checkVnDate(EndEffectedDate) == false) throw new Exception("Ngày hết hiệu lực không hợp lệ!");
                }
                #region old
                //DateTime dReleaseDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(ReleaseDate))
                //{
                //    dReleaseDate = FunctionUtilities.VNDateToDate(ReleaseDate);
                //}
                //DateTime dEffectedDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(EffectedDate))
                //{
                //    dEffectedDate = FunctionUtilities.VNDateToDate(EffectedDate);
                //}
                //DateTime dEndEffectedDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(EndEffectedDate))
                //{
                //    dEndEffectedDate = FunctionUtilities.VNDateToDate(EndEffectedDate);
                //}
                //DateTime dSignDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(SignDate))
                //{
                //    dSignDate = FunctionUtilities.VNDateToDate(SignDate);
                //}

                //DateTime dExpiredDate = new System.DateTime();
                //if (!string.IsNullOrEmpty(ExpiredDate))
                //{
                //    dExpiredDate = FunctionUtilities.VNDateToDate(ExpiredDate);
                //}

                //string NewsWorkflowControlId = WebConfig.GetConfigFromXml(OSiteParam, "NewsWorkflowControlId");
                //WorkflowDefCls OWorkflowDef = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateWorkflowDefFromWorkflowControl(SiteId, NewsWorkflowControlId);
                //if (OWorkflowDef == null) throw new Exception("Chưa thiết lập tham số khởi tạo quy trình!");
                //string WorkflowDefId = OWorkflowDef.WorkflowDefId;
                #endregion
                IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
                IDbTransaction Trans = DBService.BeginTransaction();
                try
                {
                    CmsDataCls OCmsData = new CmsDataCls();
                    OCmsData.frkGroupDataId=GroupDataId;
                    OCmsData.ParentDataId=ParentDataId;
                    OCmsData.DataAlias=Alias; 
                    OCmsData.Subject=Subject;
                    OCmsData.OtherSubject=OtherSubject;
                    OCmsData.Description=Description;
                    OCmsData.Body=Body; 
                    OCmsData.Source=Source;
                    OCmsData.Url=Url;
                    OCmsData.Target=Target;
                    OCmsData.UploadFile=UploadFile;
                    OCmsData.SortIndex=int.Parse(SortIndex);
                    OCmsData.frkDocTypeId=DocType;
                    OCmsData.frkDocFieldId=DocField;
                    OCmsData.frkDocReleaseFromId=DocReleaseFrom;
                    OCmsData.frkDocSignerId=DocSigner;
                    OCmsData.DocStatus=DocStatus;
                    OCmsData.SignDate=SignDate;
                    OCmsData.ReleaseDate=ReleaseDate;
                    OCmsData.EffectedDate=EffectedDate;
                    OCmsData.EndEffectedDate=EndEffectedDate;
                    OCmsData.ReplaceDoc=ReplaceDoc;
                    OCmsData.ModifyDoc=ModifyDoc;
                    OCmsData.VoteType=VoteType;
                    OCmsData.IsNew=IsNew;
                    OCmsData.AttachedSearchable=AttachedSearchable;
                    OCmsData.Categories=Categories;
                    OCmsData.CategoryIds=CategoryIds;
                    OCmsData.HotCategoryIds=HotCategoryIds;
                    OCmsData.NewsTypeId=NewsTypeId;
                    OCmsData.ImageLevelId=ImageLevelId;
                    OCmsData.EventId=EventId;
                    try
                    {
                        OCmsData.ExpImageCount = int.Parse(ExpImageCount);
                    }
                    catch { }
                    OCmsData.ExpImageLevelId=ExpImageLevelId;
                    OCmsData.NewsLevelId=NewsLevelId;
                    try
                    {
                        OCmsData.IsFocus = int.Parse(IsFocus);
                    }
                    catch { }
                    try
                    {
                        OCmsData.IsShowAuthor = int.Parse(IsShowAuthor);
                    }
                    catch { }
                    try
                    {
                        OCmsData.IsAdv = int.Parse(IsAdv);
                    }
                    catch { }
                    OCmsData.Keyword=Keyword;
                    OCmsData.SurveyCode=SurveyCode;
                    OCmsData.Note=Note;
                    OCmsData.ExpiredDate=ExpiredDate;

                    DataId = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).AddCmsData(Trans, SiteId, WorkflowDefId, DataLanguage, DataId, OCmsData, false);
                    if (!string.IsNullOrEmpty(WorkflowDefId))
                    {
                        //khoi tao quy trinh o day...!
                        CmsUtility.ServerSideStartWorkflow(Trans, SiteId, WorkflowDefId, DataId, "Publishing",OGroupData.frkDataTypeId,"");
                    }
                    Trans.Commit();
                    //OAjaxOut.RetUrl = "PreviewCmsData.aspx?gid=" + OGroupData.GroupDataId + "&CmsDataId=" + DataId;
                    OAjaxOut.RetUrl = "InputData.aspx?gid=1";
                }
                catch (Exception ex)
                {
                    Trans.Rollback();
                    throw (ex);
                }
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }

    }
}
